{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "da73c0e0-40a2-403f-9a7b-644ca7098f30",
   "metadata": {},
   "source": [
    "from torch.utils.data import DataLoader\n",
    "import matplotlib.pylab as plt\n",
    "from torchvision import datasets, transforms, utils"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3a2cab27-07ac-4c83-a29c-52fc6fe2c2da",
   "metadata": {},
   "source": [
    "transformers = transforms.Compose(\n",
    "    [transforms.Resize(size=28), transforms.ToTensor()]\n",
    ")\n",
    "train_data = datasets.FashionMNIST(root='../data/FashionMNIST',\n",
    "                                   train=True,\n",
    "                                   transform=transformers,\n",
    "                                   download=True)\n",
    "train_loader = DataLoader(dataset=train_data,\n",
    "                          batch_size=64,\n",
    "                          shuffle=True,\n",
    "                          # num_workers=8\n",
    "                          )"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "de31a9ba-c655-4c20-aea5-586e6dad3f2b",
   "metadata": {},
   "source": [
    "# 取出第1个批次的数据\n",
    "classes = train_data.classes\n",
    "batch1, label = next(iter(train_loader))"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cf6dd0d0-6f71-4740-9a72-5a0811845e2b",
   "metadata": {},
   "source": [
    "print(batch1.shape)\n",
    "img = batch1[0][0]\n",
    "label1 = label[0]\n",
    "print(label1.item(), classes[label1])\n",
    "img = utils.make_grid(img, padding=2)\n",
    "img = img.numpy().transpose(1,2,0)\n",
    "plt.imshow(img)\n",
    "plt.show()\n",
    "img1 = batch1.squeeze().numpy()\n",
    "print(img1.shape)"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "89405cad-daf2-4523-a33b-8eb4ad83aea9",
   "metadata": {},
   "source": [
    "print(label)\n",
    "for la in label:\n",
    "    print(classes[la], end=',')"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ec2f6c09-022d-416b-b993-b58d9db470bf",
   "metadata": {},
   "source": [
    "img = utils.make_grid(batch1, padding=2)\n",
    "print(len(label))\n",
    "print('转换前形状：', img.shape)\n",
    "# .T 属性是 numpy.transpose 的简写形式，用于快速转置二维数组。\n",
    "# .T 只能用于二维数组的转置，而 numpy.transpose 可以处理任意维度的数组。\n",
    "img = img.numpy().transpose(1,2,0) # 1,2,0表示原来的维度按照这个顺序重新排列\n",
    "print('转换后形状：', img.shape)\n",
    "plt.imshow(img)\n",
    "plt.show()\n",
    "for i in range(len(label)):\n",
    "    print(classes[label[i].item()], end=',')\n",
    "    if (i+1) % 8 == 0:\n",
    "        print()"
   ],
   "outputs": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
